%% Find Stimulus On/Off segments
function [on off] = findStimulusOnOff(events, varargin)
args = finputcheck(varargin, ...
    { 'intermittendOnEnabled',  'boolean', [], 1; ...
      'intermittendOffEnabled', 'boolean', [], 1});
if ischar(args)
    error(args);
end

blocks = findBlocks(events);

%% Display the blocks overview
disp('Blocks:');
for i = 1:length(blocks)
    endTxt = 'end';
    if ~isnan(blocks(i).End) endTxt = num2str(blocks(i).End); end;
    fprintf('  block %d: %s (%d - %s)\n', i, blocks(i).Type, blocks(i).Begin, endTxt);
end

%% Find the On/Off segments
on = [];
off = [];
for i=1:length(blocks)
    switch(blocks(i).Type)
        case 'Continuous'
            [xOn xOff] = findSphereContinuousOnOff(blocks(i));
        case 'Intermittend'
            [xOn xOff] = findSphereIntermittendOnOff(blocks(i));
            if (~args.intermittendOnEnabled)
                xOn = []; % Don't use the xOn segments of intermittend blocks
            end
            if (~args.intermittendOffEnabled)
                xOff = []; % Don't use the xOff segments of intermittend blocks
            end
        case 'Rest'
            [xOn xOff] = findRestOnOff(blocks(i));
    end
    on = [on;xOn];
    off = [off;xOff];
end
end
%% Find all blocks in the event list
function block = findBlocks(events)

stimulusMarkers = strmatch('S-', {events(:).type}); % Only keep the stimulus markers

block(1).Begin = events(stimulusMarkers(1)).latency;
block(1).Type = 'Continuous'; % Assume the first one is always a continuous block
for i = 2 : length(stimulusMarkers)-1
% Blocks start in .. ways:
%  1 - At the beginning of the list
%       (--> done in init of blocks...)
%  2 - When the off marker is more than 30 seconds after the on, its a cont.
    latencyDiffWithNextStimulus = events(stimulusMarkers(i+1)).latency - events(stimulusMarkers(i)).latency;
    if latencyDiffWithNextStimulus > 30*512
        block(end).End = events(stimulusMarkers(i)).latency - 1;
        block(end+1).Begin = events(stimulusMarkers(i)).latency;
        block(end).Type = 'Continuous';
        continue
    end
%  3 - The previous stimulus was a rest
    if strcmp(events(stimulusMarkers(i-1)).type, 'S-Rest-10')
        block(end).End = events(stimulusMarkers(i)).latency - 1;
        block(end+1).Begin = events(stimulusMarkers(i)).latency;
        block(end).Type = 'Intermittend';
        continue
    end
%  4 - Rest marker
    if strcmp(events(stimulusMarkers(i)).type, 'S-Rest-10')
        block(end).End = events(stimulusMarkers(i)).latency - 1;
        block(end+1).Begin = events(stimulusMarkers(i)).latency;
        block(end).Type = 'Rest';
        continue
    end    
end
block(end).End = NaN;

for i = 1:length(block)
    block(i).events = events( ...
        (block(i).Begin<=[events(:).latency]) & ...
        ([events(:).latency]<block(i).End));
end


end
%%
function [xOn xOff] = findSphereContinuousOnOff(block)
xOn = [block.Begin, block.End];
xOff = [];
end
%%
function [xOn xOff] = findSphereIntermittendOnOff(block)
onMarkers = strcmp('S-SI-On', {block.events(:).type});
onMarkers(1) = true; % Start marker is the first ON.
offMarkers = strcmp('S-SI-Off', {block.events(:).type});
xOn = []; xOff = [];
more = true;
while (more)
    fOn = findFirst(onMarkers);
    fOff = findFirst(offMarkers);
    if isempty(fOn)
        if ~isnan(block.End)
            xOff = [xOff; block.events(fOff).latency, block.End];
        end
        more = false;
    elseif isempty(fOff)
        if ~isnan(block.End)
            xOn = [xOn; block.events(fOn).latency, block.End];
        end
        more = false;
    elseif (fOn<fOff)
        xOn = [xOn; block.events(fOn).latency, block.events(fOff).latency-1];
        onMarkers(fOn) = false;
    else
        xOff = [xOff; block.events(fOff).latency, block.events(fOn).latency-1];
        offMarkers(fOff) = false;
    end
end
end
function idx = findFirst(logicalArray)
x = find(logicalArray);
if ~isempty(x)
    idx = x(1);
else
    idx = [];
end
end
%%
function [xOn xOff] = findRestOnOff(block)
xOn = [];
xOff = [block.Begin, block.End];
end